package xcmg.device.service.company;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.company.SupplierClassDO;
import xcmg.device.dao.mapper.SupplierClassMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.company.SupplierClassVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by sqq on 2017/12/21.
 */
@Service
public class SupplierClassService {

    @Autowired
    private SupplierClassMapper supplierClassMapper;

    public ErrorCode insert(SupplierClassVO supplierClassVO){
        supplierClassVO.setCreateDate(new Date());
        supplierClassVO.setUpdateDate(new Date());
        supplierClassVO.setIsDel(false);
        supplierClassVO.setClassValue(getClassValue(supplierClassVO.getpId(),supplierClassVO.getOrgId()));
        SupplierClassDO supplierClassDO=getDO(supplierClassVO);
        Integer classNameCount=supplierClassMapper.classNameCount(supplierClassDO);
        if(classNameCount>0){
            return ErrorCode.ClassNameDuplicated;
        }
        ErrorCode ret= supplierClassMapper.insert(supplierClassDO)>0?ErrorCode.Success:ErrorCode.Failure;
        supplierClassVO.setId(supplierClassDO.getId());
        return ret;
    }

    public ErrorCode update(SupplierClassVO supplierClassVO){
        SupplierClassVO newVO=new SupplierClassVO();
        newVO.setId(supplierClassVO.getId());
        newVO.setClassCode(supplierClassVO.getClassCode());
        newVO.setClassName(supplierClassVO.getClassName());
        newVO.setUpdateId(supplierClassVO.getUpdateId());
        newVO.setUpdateDate(new Date());
        SupplierClassDO supplierClassDO=supplierClassMapper.item(supplierClassVO.getId());
        if(supplierClassDO==null){
            return ErrorCode.IllegalArument;
        }
        supplierClassDO.setClassName(supplierClassVO.getClassName());
        Integer classNameCount=supplierClassMapper.classNameCount(supplierClassDO);
        if(classNameCount>0){
            return ErrorCode.ClassNameDuplicated;
        }
        return supplierClassMapper.update(getDO(newVO))>0?ErrorCode.Success:ErrorCode.Failure;
    }

    public ErrorCode delete(SupplierClassVO supplierClassVO){

        SupplierClassDO supplierClassDO=supplierClassMapper.item(supplierClassVO.getId());
        if(supplierClassDO==null){
            return ErrorCode.IllegalArument;
        }

        return supplierClassMapper.deleteByValue(supplierClassDO.getOrgId(),supplierClassDO.getClassValue())>0?ErrorCode.Success:ErrorCode.Failure;
    }

    public List<SupplierClassVO> list(SupplierClassVO supplierClassVO){
        supplierClassVO.setIsDel(false);
        List<SupplierClassDO> listDO=supplierClassMapper.list(supplierClassVO);
        List<SupplierClassVO> listVO=new ArrayList<>();
        if(listDO==null){
            return listVO;
        }
        for (SupplierClassDO supplierClassDO:listDO
             ) {
            listVO.add(getVO(supplierClassDO));
        }
        return listVO;
    }

    private SupplierClassDO getDO(SupplierClassVO supplierClassVO){
        if(supplierClassVO==null){return null;}
        SupplierClassDO supplierClassDO=new SupplierClassDO();
        supplierClassDO.setId(supplierClassVO.getId());
        supplierClassDO.setOrgId(supplierClassVO.getOrgId());
        supplierClassDO.setpId(supplierClassVO.getpId());
        supplierClassDO.setClassName(supplierClassVO.getClassName());
        supplierClassDO.setClassCode(supplierClassVO.getClassCode());
        supplierClassDO.setClassValue(supplierClassVO.getClassValue());
        supplierClassDO.setIsDel(supplierClassVO.getIsDel());
        supplierClassDO.setCreateDate(supplierClassVO.getCreateDate());
        supplierClassDO.setCreateId(supplierClassVO.getCreateId());
        supplierClassDO.setUpdateDate(supplierClassVO.getUpdateDate());
        supplierClassDO.setUpdateId(supplierClassVO.getUpdateId());
        return supplierClassDO;
    }

    private SupplierClassVO getVO(SupplierClassDO supplierClassDO){
        if(supplierClassDO==null){return null;}
        SupplierClassVO supplierClassVO=new SupplierClassVO();
        supplierClassVO.setId(supplierClassDO.getId());
        supplierClassVO.setOrgId(supplierClassDO.getOrgId());
        supplierClassVO.setpId(supplierClassDO.getpId());
        supplierClassVO.setClassName(supplierClassDO.getClassName());
        supplierClassVO.setClassCode(supplierClassDO.getClassCode());
        supplierClassVO.setClassValue(supplierClassDO.getClassValue());
        supplierClassVO.setIsDel(supplierClassDO.getIsDel());
        supplierClassVO.setCreateDate(supplierClassDO.getCreateDate());
        supplierClassVO.setCreateId(supplierClassDO.getCreateId());
        supplierClassVO.setUpdateDate(supplierClassDO.getUpdateDate());
        supplierClassVO.setUpdateId(supplierClassDO.getUpdateId());
        supplierClassVO.setChildCount(supplierClassDO.getChildCount());
        return supplierClassVO;
    }

    private String getClassValue(Long pId,String orgId){
        String classValue=supplierClassMapper.getMaxValue(pId,orgId);
        if(classValue==null||classValue==""){
            if(pId==0){
                classValue="001";
            }else {
                SupplierClassDO supplierClassDO=supplierClassMapper.item(pId);
                classValue=supplierClassDO.getClassValue()+"001";
            }
        }else {
            String endValue= classValue.substring(classValue.length()-3,classValue.length());
            String startValue= classValue.substring(0,classValue.length()-3);
            Integer intValue= Integer.parseInt(endValue)+1;
            if(intValue<10){
                classValue=startValue+"00"+String.valueOf(intValue);
            }if(intValue>=10 && intValue<100){
                classValue=startValue+"0"+String.valueOf(intValue);
            }if(intValue>=100){
                classValue=startValue+String.valueOf(intValue);
            }
        }
        return classValue;
    }

}
